const db = require("../../../db");

const search = (req, res) => {
  let { account, type } = req.body;
  let sql = `select * from user where account=${account} and `;
  if (type === "qq") {
    sql += `qq='${account}'`;
  } else if (type === "email") {
    sql += `email='${account}'`;
  } else {
    sql += `phoneNumber='${account}'`;
  }
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (result.length === 0) {
      return res.send({ code: 0, message: "暂无好友", data: [] });
    }
    res.send({ code: 0, message: "获取成功", data: result[0] });
  });
};

const userInfo = (req, res) => {
  let { userId } = req.body;
  let sql = `select * from user where id=${userId}`;
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (result.length === 0) {
      return res.send({ code: 1, message: "获取失败", data: [] });
    }
    res.send({ code: 0, message: "获取成功", data: result[0] });
  });
};

module.exports = { search, userInfo };
